package com.tuanzi.loan.business.repository.businesssystem.impl;

import com.tuanzi.loan.business.repository.businesssystem.custom.BusinessBillRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.businesssystem.BusinessBillRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessBillResponse;
import org.apache.commons.lang3.time.DateUtils;
import org.jooq.Condition;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessBill.LOAN_BUSINESS_BILL;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessChannelMain.LOAN_BUSINESS_CHANNEL_MAIN;


public class BusinessBillRepositoryImpl extends JOOQBaseRepositoryPlus implements BusinessBillRepositoryCustom {
    @Override
    public Page<BusinessBillResponse> list(BusinessBillRequest searchRequest, Pageable pageable) {
        Long companyId = searchRequest.getCompanyId();
        Date startTime = searchRequest.getStartTime();
        String creator = searchRequest.getCreator();
        Integer paymentMain = searchRequest.getPaymentMain();
        Integer billStatus = searchRequest.getBillStatus();
        Date endTime = searchRequest.getEndTime();
        if (endTime != null){
            endTime = DateUtils.addHours(endTime,23);
            endTime = DateUtils.addMinutes(endTime,59);
            endTime = DateUtils.addSeconds(endTime,59);
        }
        fields(LOAN_BUSINESS_BILL.fields());
        fields(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN,LOAN_BUSINESS_CHANNEL_MAIN.BILL_AMOUNT.as("allBill")
                ,LOAN_BUSINESS_CHANNEL_MAIN.EXPEND_AMOUNT,LOAN_BUSINESS_CHANNEL_MAIN.PREPAY_AMOUNT,
                LOAN_BUSINESS_CHANNEL_MAIN.MAIN_TYPE);
        leftJoin(LOAN_BUSINESS_CHANNEL_MAIN,LOAN_BUSINESS_CHANNEL_MAIN.ID.eq(LOAN_BUSINESS_BILL.COMPANY_ID.cast(Integer.class)));
        eq(LOAN_BUSINESS_BILL.COMPANY_ID,companyId);
        eq(LOAN_BUSINESS_BILL.CREATOR,creator);
        eq(LOAN_BUSINESS_BILL.PAYMENT_MAIN,paymentMain);
        between(LOAN_BUSINESS_BILL.CREATE_TIME,startTime,endTime);
        orderBy(LOAN_BUSINESS_BILL.CREATE_TIME, Sort.Direction.DESC);

        Page<BusinessBillResponse> search = search(pageable, record -> {
            BusinessBillResponse response = new BusinessBillResponse();
            response.setId(record.get(LOAN_BUSINESS_BILL.ID));
            response.setCompanyName(record.get(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN));
            response.setMainType(record.get(LOAN_BUSINESS_CHANNEL_MAIN.MAIN_TYPE,Integer.class));
            Double prepayAmount = record.get(LOAN_BUSINESS_CHANNEL_MAIN.PREPAY_AMOUNT,Double.class);
            response.setBillType(record.get(LOAN_BUSINESS_BILL.BILL_TYPE,Integer.class)==null?"普票":record.get(LOAN_BUSINESS_BILL.BILL_TYPE,Integer.class)==1?"普票":record.get(LOAN_BUSINESS_BILL.BILL_TYPE,Integer.class)==2?"专票":"电子发票");
            response.setPrepayAmount(prepayAmount);
            response.setPaymentMain(record.get(LOAN_BUSINESS_BILL.PAYMENT_MAIN));
            response.setBillAmount(record.get(LOAN_BUSINESS_BILL.BILL_AMOUNT));
            response.setBillDate(record.get(LOAN_BUSINESS_BILL.BILL_DATE));
            response.setBillNumber(record.get(LOAN_BUSINESS_BILL.BILL_NUMBER));
            Double lastAmount = record.get("allBill",Double.class);
            response.setLastAmount(prepayAmount-lastAmount);
            response.setBillStatus(record.get(LOAN_BUSINESS_BILL.BILL_STATUS,Integer.class));
            response.setRemark(record.get(LOAN_BUSINESS_BILL.REMARK));
            response.setCreator(record.get(LOAN_BUSINESS_BILL.CREATOR));
            response.setCreateTime(record.get(LOAN_BUSINESS_BILL.CREATE_TIME));
            response.setUpdator(record.get(LOAN_BUSINESS_BILL.UPDATOR));
            response.setUpdateTime(record.get(LOAN_BUSINESS_BILL.UPDATE_TIME));
            return response;
        });

        return search;
    }

    @Override
    public Double getAllIncome(BusinessBillRequest $) {
        Long companyId = $.getCompanyId();
        Integer paymentMain = $.getPaymentMain();
        Date startTime = $.getStartTime();
        Date endTime = $.getEndTime();
        Condition condition = LOAN_BUSINESS_BILL.ID.isNotNull();
        if (companyId!=null)
            condition=condition.and(LOAN_BUSINESS_BILL.COMPANY_ID.eq(companyId));
        if (startTime!=null&&endTime!=null)
            condition=condition.and(LOAN_BUSINESS_BILL.BILL_DATE.ge(new java.sql.Date(startTime.getTime()))).and(LOAN_BUSINESS_BILL.BILL_DATE.le(new java.sql.Date(endTime.getTime())));
        if(paymentMain!=null)
            condition = condition.and(LOAN_BUSINESS_BILL.PAYMENT_MAIN.eq(paymentMain));
        Double into = dslContext.select(DSL.sum(LOAN_BUSINESS_BILL.BILL_AMOUNT)).from(LOAN_BUSINESS_BILL).leftJoin(LOAN_BUSINESS_CHANNEL_MAIN)
                .on(LOAN_BUSINESS_CHANNEL_MAIN.ID.eq(LOAN_BUSINESS_BILL.COMPANY_ID.cast(Integer.class)))
                .where(condition).fetchOne().into(Double.class);
        return into;

    }

    @Override
    protected TableImpl getTable() {
        return LOAN_BUSINESS_BILL;
    }
}
